package com.example.artspace

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.widthIn
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.artspace.ui.theme.ArtSpaceTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ArtSpaceTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    ArtAlbum()
                }
            }
        }
    }
}

@Composable
fun ArtAlbum() {
    var id by remember {
        mutableIntStateOf(1)
    }
    var author by remember {
        mutableIntStateOf(R.string.author_1)
    }
    var desc by remember {
        mutableIntStateOf(R.string.desc_1)
    }
    var date by remember {
        mutableIntStateOf(R.string.date_1)
    }
    var picture by remember {
        mutableIntStateOf(R.drawable.artspace1)
    }
    val onClick = {
        if (id == 1) {
            id = 2
            picture = R.drawable.artspace2
            author = R.string.author_2
            desc = R.string.desc_2
            date = R.string.date_2
        } else {
            id = 1
            picture = R.drawable.artspace1
            author = R.string.author_1
            desc = R.string.desc_1
            date = R.string.date_1
        }
    }
    Column(
        verticalArrangement = Arrangement.Bottom,
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier.padding(20.dp)
    ) {
        ArtPicture(picture)
        Spacer(modifier = Modifier.height(40.dp))
        ArtDesc(author = author, desc = desc, date = date)
        ArtButton(onClick, onClick)
    }
}

@Composable
fun ArtPicture(pic: Int, modifier: Modifier = Modifier) {
    Image(
        painter = painterResource(id = pic),
        contentDescription = "Water",
        contentScale = ContentScale.Fit,
        modifier = modifier
            .padding(bottom = 20.dp)
            .size(width = 400.dp, height = 480.dp)
            .border(border = BorderStroke(35.dp, Color.White))
            .shadow(10.dp)
    )
}

@Composable
fun ArtDesc(author: Int, desc: Int, date: Int, modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .widthIn(max = 410.dp)
            .background(color = Color.LightGray)
            .padding(10.dp)
    ) {
        Row(
            modifier = modifier.padding(bottom = 10.dp)
        ) {
            Text(text = stringResource(desc), fontSize = 25.sp)
        }
        Row {
            Text(text = stringResource(author), fontWeight = FontWeight.Bold)
            Text(text = "(" + stringResource(date) + ")")
        }
    }
}

@Composable
fun ArtButton(
    preClick: () -> Unit,
    nextClick: () -> Unit
) {
    Row(
        verticalAlignment = Alignment.Bottom,
        horizontalArrangement = Arrangement.SpaceBetween,
        modifier = Modifier
            .fillMaxWidth()
            .padding(top = 20.dp)
    ) {
        Button(onClick = preClick, Modifier.width(150.dp)) {
            Text(text = "Previous")
        }
        Button(onClick = nextClick, Modifier.width(150.dp)) {
            Text(text = "Next")
        }
    }
}

@Preview(showBackground = true, showSystemUi = true)
@Composable
fun GreetingPreview() {
    ArtSpaceTheme {
        ArtAlbum()
    }
}
